Can a sealed class be used as a base class?
Can a sealed class be used as a base class?
410
27-Aug-2021
Aryan Kumar
24-Jun-2023No, a sealed class cannot be used as a base class. A sealed class is a class that cannot be inherited from. This means that you cannot create a subclass of a sealed class.
The
sealedmodifier is used to prevent derivation from a class. An error occurs if a sealed class is specified as the base class of another class.Here is an example of a sealed class in C#:
C#
In this example, the
SealedClassclass cannot be inherited from. If you try to create a subclass of theSealedClassclass, you will get an error.There are a few reasons why a sealed class might be used. One reason is to prevent the creation of subclasses that could potentially break the code. Another reason is to improve performance by preventing the compiler from having to generate code for potential subclasses.
If you need to create a class that cannot be inherited from, you can use the
sealedmodifier. This will prevent other developers from creating subclasses of your class, which can help to improve the safety and performance of your code.